package com.towker.core.util;

//Util��
//Java����  �ղش���

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
  
/** 
 * Oracle��ݿ����� 
 *  
 * @author *** 
 */  
public class DBUtils {  
  
    private Connection conn = null;  
    private Statement stmt = null;  
    private ResultSet rs = null;  
  
    /** Oracle��ݿ����� URL */  
    private final static String DB_URL = "jdbc:oracle:thin:@localhost:1521:thzx";  
  
    /** Oracle��ݿ������� */  
    private final static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";  
  
    /** ��ݿ��û��� */  
    private final static String DB_USERNAME = "cgd";  
  
    /** ��ݿ����� */  
    private final static String DB_PASSWORD = "cgd";  
  
    /** 
     * ��ȡ��ݿ����� 
     *  
     * @return 
     */  
    public Connection getConnection() {  
        /** ����Connection���Ӷ��� */  
        Connection conn = null;  
        try {  
            /** ʹ�� Class.forName()�����Զ��������������ʵ�����Զ�����

DriverManager��ע���� */  
            Class.forName(DB_DRIVER);  
            /** ͨ�� DriverManager��getConnection()������ȡ��ݿ����� */  
            conn = DriverManager  
                    .getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);  
            stmt = conn.createStatement();  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
        return conn;  
    }  
  
    /** 
     * ��ѯ��ݲ��� 
     *  
     * @return ResultSet 
     */  
    public ResultSet executeQuery(String sqlStr) {  
        if (sqlStr == null || sqlStr.length() == 0)  
            return null;  
        try {  
            this.getConnection();  
            rs = stmt.executeQuery(sqlStr);  
            return rs;  
        } catch (SQLException ex) {  
            ex.printStackTrace();  
            return null;  
        }  
  
    }  
  
    /** 
     * ������ݲ��� 
     *  
     * @return �����Ƿ�ɹ� 
     */  
    public boolean executeUpdate(String sqlStr) {  
  
        if (sqlStr == null || sqlStr.length() == 0)  
            return false;  
        try {  
            this.getConnection();  
            stmt.executeUpdate(sqlStr);  
            return true;  
        } catch (SQLException ex) {  
            ex.printStackTrace();  
            return false;  
        } finally {  
            try {  
                if (stmt != null) {  
                    stmt.close();  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
            try {  
                if (conn != null) {  
                    conn.close();  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
  
        }  
  
    }  
  
    public void closeStmt() {  
        try {  
            if (stmt != null) {  
                stmt.close();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * �ر���ݿ����� 
     *  
     * @param connect 
     */  
    public void closeConnection() {  
        try {  
            if (conn != null) {  
                /** �жϵ�ǰ�������Ӷ������û�б��رվ͵��ùرշ��� */  
                if (!conn.isClosed()) {  
                    conn.close();  
                }  
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
}  
